/**
 * Copyright 2022-9999 the original author or authors.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.binghe.im.platform.message.controller;

import io.binghe.im.platform.common.response.ResponseMessage;
import io.binghe.im.platform.common.response.ResponseMessageFactory;
import io.binghe.im.platform.message.application.ice.ICEServer;
import io.binghe.im.platform.message.application.service.WebrtcService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author binghe(微信 : hacker_binghe)
 * @version 1.0.0
 * @description Web RTC
 * @github https://github.com/binghe001
 * @copyright 公众号: 冰河技术
 */
@Api(tags = "webrtc视频通话")
@RestController
@RequestMapping("/webrtc/private")
public class WebrtcController {

    @Autowired
    private WebrtcService webrtcService;

    @ApiOperation(httpMethod = "POST", value = "呼叫视频通话")
    @PostMapping("/call")
    public ResponseMessage<String> call(@RequestParam Long uid, @RequestBody String offer) {
        webrtcService.call(uid,offer);
        return ResponseMessageFactory.getSuccessResponseMessage();
    }

    @ApiOperation(httpMethod = "POST", value = "取消呼叫")
    @PostMapping("/cancel")
    public ResponseMessage<String> cancel(@RequestParam Long uid) {
        webrtcService.cancel(uid);
        return ResponseMessageFactory.getSuccessResponseMessage();
    }

    @ApiOperation(httpMethod = "POST", value = "呼叫失败")
    @PostMapping("/failed")
    public ResponseMessage<String> failed(@RequestParam Long uid,@RequestParam String reason) {
        webrtcService.failed(uid,reason);
        return ResponseMessageFactory.getSuccessResponseMessage();
    }

    @ApiOperation(httpMethod = "POST", value = "接受视频通话")
    @PostMapping("/accept")
    public ResponseMessage<String> accept(@RequestParam Long uid,@RequestBody String answer) {
        webrtcService.accept(uid,answer);
        return ResponseMessageFactory.getSuccessResponseMessage();
    }

    @ApiOperation(httpMethod = "POST", value = "拒绝视频通话")
    @PostMapping("/reject")
    public ResponseMessage<String> reject(@RequestParam Long uid) {
        webrtcService.reject(uid);
        return ResponseMessageFactory.getSuccessResponseMessage();
    }

    @ApiOperation(httpMethod = "POST", value = "挂断")
    @PostMapping("/handup")
    public ResponseMessage<String> leave(@RequestParam Long uid) {
        webrtcService.leave(uid);
        return ResponseMessageFactory.getSuccessResponseMessage();
    }

    @PostMapping("/candidate")
    @ApiOperation(httpMethod = "POST", value = "同步candidate")
    public ResponseMessage<String> forwardCandidate(@RequestParam Long uid,@RequestBody String candidate ) {
        webrtcService.candidate(uid,candidate);
        return ResponseMessageFactory.getSuccessResponseMessage();
    }

    @GetMapping("/iceservers")
    @ApiOperation(httpMethod = "GET", value = "获取iceservers")
    public ResponseMessage<List<ICEServer>>  iceservers() {
        return ResponseMessageFactory.getSuccessResponseMessage(webrtcService.getIceServers());
    }
}
